package com.qhit.controller;

import com.qhit.pojo.Tourist;
import com.qhit.service.TouristService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/tourist")
public class TouristController {
    @Autowired
    private TouristService touristService;
    @RequestMapping("/list")
    public String list(Model m,String keywords,
                       @RequestParam(defaultValue = "1",required = false)
                       Integer page,
                       @RequestParam(defaultValue = "1",required = false)
                       Integer pageSize){
        HashMap hashMap = new HashMap<>();
        hashMap.put("keywords",keywords);
        hashMap.put("start",(page-1)*pageSize);
        hashMap.put("pageSize",pageSize);
        hashMap.put("page",page);
        List<Tourist> touristList=touristService.getList(hashMap);
        int total= touristService.getListCount(hashMap);
        hashMap.put("totalPage",(total%pageSize==0)?(total/pageSize):(total/pageSize+1));
        m.addAttribute("touristList",touristList);
        m.addAttribute("myParams",hashMap);
        return "tourist/list";
    }
    @RequestMapping("toAdd")
    public String toAdd(){
        return "tourist/add";
    }
    @RequestMapping(value = "/add",method =RequestMethod.POST)
    public String add(Tourist tourist, Model m,String touristBirthdayString){
        tourist.setId(UUID.randomUUID().toString());
        if (touristBirthdayString !=null&&!"".equals(touristBirthdayString)){

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date parse = null;
            try {
                parse = simpleDateFormat.parse(touristBirthdayString);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            tourist.setTouristBirthday(parse);
        }

       int r= this.touristService.add(tourist);
        if (r>0){
            m.addAttribute("msg","添加成功！！");
        }else {
            m.addAttribute("msg","添加失败！！");
        }
        return "tourist/msg";
    }
    @RequestMapping("/toEdit")
    public String toEdit(String id,Model model){
        Tourist g=touristService.getTouristById(id);
        model.addAttribute("tourist",g);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        model.addAttribute("birthdayString",simpleDateFormat.format(g.getTouristBirthday()));
        return "tourist/edit";
    }
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public String edit(Tourist g,Model model,String touristBirthdayString){

        if (touristBirthdayString !=null&&!"".equals(touristBirthdayString)){

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date parse = null;
            try {
                parse = simpleDateFormat.parse(touristBirthdayString);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            g.setTouristBirthday(parse);
        }

        int re=touristService.edit(g);
        if (re>0){
            model.addAttribute("msg","修改成功！！");
        }else {
            model.addAttribute("msg","修改失败！！");
        }
        return "tourist/msg";
    }
    @RequestMapping(value = "/del",method = RequestMethod.GET)
    public String del(String id,Model model){

        int re=touristService.del(id);
        if (re>0){
            model.addAttribute("msg","删除成功！！");
        }else {
            model.addAttribute("msg","删除失败！！");
        }
        return "tourist/msg";
    }
}
